package com.stars.controller.pshow;

import com.stars.entity.dto.pas.ChartPurchaseDTO;
import com.stars.entity.dto.pshow.PShowOrdersDTO;
import com.stars.services.pshow.PShowOrdersConfigServices;
import com.stars.entity.pshow.PShowOrdersEntity;
import com.stars.services.pshow.PShowOrdersAccessServices;
import com.stars.entity.pshow.PShowOrdersGridJson;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import com.stars.common.annotation.Log;
import com.stars.common.base.BaseMsg;
import com.stars.common.ObjectCopyUtils;
import com.stars.common.base.BaseConstant;
import com.stars.common.base.ResultMsg;
import com.stars.controller.BaseController;
import com.stars.common.enums.OperationType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.stars.common.valid.AddView;
import com.stars.common.valid.UpdateView;
import com.github.pagehelper.PageInfo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import com.stars.common.annotation.AutoCodeClass;
/**
 * @Title: 销售订单 mybatis dao
 * @author 肖伟
 * @date 2020-01-04
 * @version V1.0
 *
 */
@Slf4j
@RestController
@RequestMapping("/pShowOrdersCtl")
@Api(tags ="销售订单")
public class PShowOrdersCtl extends BaseController{
   @Autowired
   private PShowOrdersAccessServices  pShowOrdersAccessServicesImpl;
   @Autowired
   private PShowOrdersConfigServices  pShowOrdersConfigServicesImpl;

   /**
   *  分页查询
   * @param pageNum 起始页
   * @param pageSize 大小
   * @return 分页数据
   * @throws Exception
   */
   @GetMapping("/pageList")
   @ApiOperation(value = "销售订单"+BaseMsg.MODULAR_PAGE,notes ="销售订单"+ BaseMsg.MODULAR_PAGE)
   @Log(detail = "销售订单"+ BaseMsg.MODULAR_PAGE,operationType = OperationType.SELECT)
   public PageInfo<PShowOrdersDTO> pagePShowOrdersList(Integer pageNum, Integer pageSize, PShowOrdersDTO dto,HttpServletRequest request) throws Exception{
  
        return pShowOrdersAccessServicesImpl.pageList(pageNum,pageSize,dto);

   }
    /**
    * 保存
    * @param entityName
    * @return 成功失败
    * @throws Exception
    */
    @PostMapping(value = "/add")
	@AutoCodeClass
    @ApiOperation(value = "销售订单"+ BaseMsg.MODULAR_ADD,notes = "销售订单"+BaseMsg.MODULAR_ADD)
    @Log(detail = "销售订单"+BaseMsg.MODULAR_ADD,operationType = OperationType.INSERT)
    public ResultMsg addPShowOrders(@Validated({AddView.class}) PShowOrdersEntity entityName,PShowOrdersGridJson gridJson,HttpServletRequest request) throws Exception{
        setAddBase(entityName,request,null);
        return pShowOrdersConfigServicesImpl.addMain(entityName,gridJson);
    }
	 /**
     * 删除
     * @param ids
     * @return 成功失败
     * @throws Exception
     */
     @PostMapping(value = "/delById")
     @ApiOperation(value = "销售订单"+BaseMsg.MODULAR_DEL,notes ="销售订单"+BaseMsg.MODULAR_DEL)
     @Log(detail ="销售订单"+BaseMsg.MODULAR_DEL,operationType = OperationType.DELETE)
     public ResultMsg delPShowOrdersById(String ids) throws Exception{
       return pShowOrdersConfigServicesImpl.delMainAndInfo(ids.split(BaseConstant.SPOT));
     }
    /**
    * 根据id查询
    * @param id
    * @return 查询对象
    * @throws Exception
    */
    @GetMapping(value = "/findById")
    @ApiOperation(value = "销售订单"+ BaseMsg.MODULAR_FIND_ID,notes = "销售订单"+BaseMsg.MODULAR_FIND_ID)
    @Log(detail = "销售订单"+ BaseMsg.MODULAR_FIND_ID,operationType = OperationType.SELECT)
    public ResultMsg findPShowOrdersById(String id) throws Exception{
        PShowOrdersEntity entityName = pShowOrdersAccessServicesImpl.selectById(id);
         return ResultMsg.success(entityName);
    }

    @PostMapping(value = "/chartOrdersStatus")
    @ApiOperation(value = "订单状态统计",notes ="销售订单"+BaseMsg.MODULAR_SEARCH)
    @Log(detail ="销售订单",operationType = OperationType.SELECT)
    public ResultMsg chartSaleOrdersStatus(ChartPurchaseDTO dto, String chartType)throws Exception{
        return pShowOrdersAccessServicesImpl.countByTableColumns("P_SHOW_ORDERS t","ORDER_STATUS",addSql(dto),chartType);
    }


    @PostMapping(value = "/chartCustomerOrderNumAll")
    @ApiOperation(value = "订单客户物品数量统计总数，完成，未完成",notes ="销售订单"+BaseMsg.MODULAR_SEARCH)
    @Log(detail ="销售订单",operationType = OperationType.SELECT)
    public ResultMsg chartCustomerOrderNumAll(ChartPurchaseDTO dto, String chartType)throws Exception{
        Map<String,String> col = new HashMap<>();
        col.put("t.ORDER_NUM","总数");
        col.put("t.ORDER_DONE_NUM","完成");
        return pShowOrdersAccessServicesImpl.sumByTableColumns("P_SHOW_ORDERS t LEFT JOIN P_SHOW_CUSTOMER s ON t.CUSTOMER_ID = s.ID","s.CUSTOMER_NAME",col,addSql(dto),chartType);
    }
    




}